home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / ms_dos / npcut / src / npcut.c next >
Encoding:
C/C++ Source or Header  |  1993-07-08  |  4.3 KB  |  233 lines

  1. /***************
  2. *
  3. * g:\exe\txf\src\npcut\npcut.c
  4. */
  5. #define MAIN 1
  6. #include "npcut.h"
  7.  
  8. void result()
  9. {
  10.     if (make_val>0) printf("\n%4d file(s) picked !\n",make_val);
  11.     else printf("\n no files picked.\n");
  12. }
  13.  
  14. void make_new_name(char *tellptr)
  15. {
  16.     char tellchr;
  17.     struct find_t filedata;
  18.  
  19.     if (tellptr == NULL) {
  20.         tellptr = strchr(outputfile, NUL) - 1;
  21.     }
  22.     tellchr = *tellptr;
  23.     tellchr++;
  24.  
  25.     if (tellchr == '\\') {
  26.         printf("Error:increment point is overrun the filename.\n");
  27.         result();
  28.         exit(1);
  29.     }
  30.     if (isalnum((char)tellchr)) {
  31.         *tellptr = tellchr;
  32.     }
  33.     else if (tellchr == ':') {
  34.         *tellptr = '0';
  35.         make_new_name(tellptr - 1);
  36.     }
  37.     else if ((tellchr=='{') || (tellchr=='[')) {
  38.         *tellptr = tellchr - 26;
  39.         make_new_name(tellptr - 1);
  40.     }
  41.     else {
  42.         printf("Error:cannot make new filename...\n");
  43.         exit(1);
  44.     }
  45.     if (_dos_findfirst(outputfile, _A_ARCH | _A_NORMAL | _A_RDONLY, &filedata)
  46.         == 0) {
  47.         make_new_name(strrchr(outputfile, '.') - 1);
  48.     }
  49. }
  50.  
  51. int findfront()
  52. {
  53.     int i=0,chr=NUL;
  54.     struct find_t filedata;
  55.  
  56.     while (i < sizeof(npcut_front)) {
  57.         chr = getc(input);
  58.         if (chr == npcut_front[i]) i++;
  59.             else if (chr == 0x0d) {
  60.                 chr = getc(input);
  61.                 if (chr != 0x0a) i = 0;
  62.             }
  63.             else i = 0;
  64.         if (chr == EOF) return (-1);
  65.     }
  66.     if (_dos_findfirst(outputfile,_A_ARCH|_A_NORMAL|_A_RDONLY,&filedata)==0) {
  67.         make_new_name(strrchr(outputfile,'.')-1);
  68.     }
  69.     if (*outputfile != NUL) {
  70.         output = fopen(outputfile, "wb");
  71.     }
  72.     else {
  73.         output = NULL;
  74.     }
  75.     if (output == NULL) {
  76.         printf("file=%s\n", outputfile);
  77.         printf("Error:cannot open outputfile\n");
  78.         exit(1);
  79.     }
  80.     return (0);
  81. }
  82.  
  83. int findtell(int endflg)
  84. {
  85.     int i=0, chr=NUL, j=0, k=0, diff=0, same=0, l=0;
  86.     char tmpbuf[24];
  87.     char *delstr[NP_DEL],*delptr[NP_DEL];
  88.  
  89.     delstr[0] = npcut_tellstr;
  90.     delstr[1] = npcut_niffirst;
  91.     delstr[2] = npcut_nifend;
  92.     delstr[3] = npcut_weather;
  93.  
  94.     for (j = 0; j < NP_DEL; j++) {
  95.         delptr[j] = delstr[j];
  96.     }
  97.  
  98.     if (endflg) return (-1);
  99.  
  100.     while (i < (sizeof(npcut_tellstr) - 1)) {
  101.         chr = getc(input);
  102.         if (chr == EOF) {
  103.             return (-1);
  104.         }
  105.         if (chr == npcut_tellstr[i]) {
  106.             i++;
  107.         }
  108.         else if (chr == 0x0d) {
  109.             putc(0x0d, output);
  110.             chr = getc(input);
  111.             if (chr != 0x0a) i=0;
  112.         }
  113.         else {
  114.             i = 0;
  115.         }
  116.  
  117.         diff = 0;
  118.         same = 0;
  119.         for (k = 0; k < NP_DEL; k++) {
  120.             if (chr == *delptr[k]) {
  121.                 delptr[k] += 1;
  122.                 if (*delptr[k] == NUL) {
  123.                     same = 1;
  124.                     delptr[k] = delstr[k];
  125.                     if (k == 3) {
  126.                         weatherflg = 1;
  127.                     }
  128.                 }
  129.             }
  130.             else {
  131.                 delptr[k] = delstr[k];
  132.                 diff++;
  133.             }
  134.         }
  135.  
  136.         if (same == 0) {
  137.             if (diff < NP_DEL) {
  138.                 tmpbuf[l] = chr;
  139.                 l++;
  140.                 tmpbuf[l] = NUL;
  141.             }
  142.             else {
  143.                 if (weatherflg == 1) {
  144.                     fclose(output);
  145.                     printf("%s\n", outputfile);
  146.                     make_val += 1;
  147.                     make_new_name(strchr(outputfile, '.') - 1);
  148.                     if((output = fopen(outputfile,"wb")) == NULL) {
  149.                         printf("Error:cannot open outputfile\n");
  150.                         exit(0);
  151.                     }
  152.                     weatherflg = 0;
  153.                 }
  154.                 if (l > 0) {
  155.                     fwrite(tmpbuf, 1, l, output);
  156.                 }
  157.                 putc((char)chr, output);
  158.                 l = 0;
  159.             }
  160.         }
  161.         else {
  162.             l = 0;
  163.         }
  164.     }
  165.  
  166.     fclose(output);
  167.     printf("%s\n", outputfile);
  168.     make_val += 1;
  169.     make_new_name(strchr(outputfile, '.') -1);
  170.  
  171.     return (0);
  172. }
  173.  
  174. void npcut()
  175. {
  176.     if (strchr(outputfile,'.') == NULL) {
  177.         strcat(outputfile,".NPB");
  178.     }
  179.  
  180.     if (*inputfile != NUL) {
  181.         input = fopen(inputfile, "rb");
  182.     }
  183.     else {
  184.         input = stdin;
  185.     }
  186.     if (input == NULL) {
  187.         printf("Error:cannot open inputfile\n");
  188.     }
  189.     while (!findtell(findfront()));
  190.     result();
  191. }
  192.  
  193. void npcutinit()
  194. {
  195.     fprintf(stderr,"NPCUT Ver1.7 copyright (c) 1991-1993 T.Nakatani\n");
  196.     strcpy(outputfile,"NPCUT000.NPB");
  197. }
  198.  
  199. void npcutdriver(char **val)
  200. {
  201. int i=0;
  202.     npcutinit();
  203.     for (i=0;val[i]!=NULL;i++) {
  204.     if (val[i][0]=='-') {
  205.         switch(val[i][1]) {
  206.             case 'i':
  207.             case 'I':
  208.                 if (val[i+1][0]!='-') strcpy(inputfile,val[i+1]);
  209.                     else i--;
  210.                 break;
  211.             case 'o':
  212.             case 'O':
  213.                 if (val[i+1][0]!='-') strcpy(outputfile,val[i+1]);
  214.                     else i--;
  215.                 break;
  216.             default:
  217.                 fprintf(stderr,"Error:invailed parameter'%s'\n",val[i]);
  218.                 exit(1);
  219.             }
  220.         i ++;
  221.         }
  222.     else if (*inputfile==NUL) strcpy(inputfile,val[i]);
  223.     else if (*inputfile!=NUL) strcpy(outputfile,val[i]);
  224.     }
  225.     npcut();
  226. }
  227.  
  228. int main(int argc,char **argv)
  229. {
  230.     npcutdriver(&argv[1]);
  231.     return (0);
  232. }
  233.